package cc.openkit.admin.controller.api;

import cc.openkit.admin.model.*;
import cc.openkit.admin.service.company.CompanyService;
import cc.openkit.admin.service.companyInfo.CompanyInfoService;
import cc.openkit.admin.service.houseRenovationOffer.HouseRenovationOfferService;
import cc.openkit.admin.service.project.ProjectService;
import cc.openkit.admin.service.shopType.ShopTypeService;
import cc.openkit.admin.service.task.TaskService;
import cc.openkit.admin.util.AppUtil;
import cc.openkit.admin.util.Pager;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.admin.vo.CompanyVo;
import cc.openkit.admin.vo.TaskVo;
import cc.openkit.common.KitUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务
 */
@Controller
@Scope("prototype")
@RequestMapping("/task")
public class TaskController {

    private Logger log = Logger.getLogger(TaskController.class);
    private Map<String,Object> map = new HashMap<String, Object>();
    @Resource
    private TaskService taskService;
    @Resource
    private HouseRenovationOfferService houseRenovationOfferService;
    @Resource
    private ShopTypeService shopTypeService;
    @Resource
    private ProjectService projectService;
    @Resource
    private CompanyInfoService companyInfoService;
    @Resource
    private CompanyService companyService;


    /**
     * 操作商户是否开工
     * @return
     */
    @RequestMapping(value = "/isWork", method = RequestMethod.POST)
    @ResponseBody
    public Object isWork(HttpServletRequest request) throws Exception{
        log.info("isWork");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //开工状态  1.开工 2.休息
        String isWork = request.getParameter("isWork");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(isWork,"开工状态");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }
        Company company = new Company();
        company.setUserId(uid);
        company.setIsWork(Integer.parseInt(isWork));

        //修改是否开工
        Integer integer = companyService.updateSelective(company);
        if (integer>0){
            map.put("code",200);
        }else{
            map.put("code",101);
        }

        return JSONObject.toJSON(map);
    }

    /**
     * 根据运营中心id查询发给运营中心的新任务
     * @return
     */
    @RequestMapping(value = "/findCenterTaskList", method = RequestMethod.POST)
    @ResponseBody
    public Object findCenterTaskList(HttpServletRequest request) throws Exception{
        log.info("findCenterTaskList");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskToUser(uid);
        task.setTaskStatus(3);//1.接收 2.拒绝 3.待接单 4.已上门 5.已完成（这条任务的状态）

        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            task.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }
        //根据用户id和接单状态查询订单
        List<TaskVo> tasks=taskService.findByUserAndTaskStatus(task);

        //新任务个数
        Integer count = taskService.findCountByNewTask(uid);

        map.put("code",200);
        map.put("tasks",tasks);
        map.put("count",count);
        return JSONObject.toJSON(map);
    }

    /**
     * 运营中心 》》收到任务和拒单
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/receiveAndRefuseTask", method = RequestMethod.POST)
    @ResponseBody
    public Object receiveAndRefuseTask(HttpServletRequest request) throws Exception{
        log.info("receiveAndRefuseTask");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }

        //获取登录人id
        String uid = request.getHeader("uid");
        //任务id
        String taskId = request.getParameter("taskId");
        //接单状态 1.接收 2.拒绝 3.派单中 4.已完成
        String taskStatus = request.getParameter("taskStatus");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(taskId,"任务接收状态");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskId(taskId);
        task.setTaskStatus(Integer.parseInt(taskStatus));
        //根据主键修改任务状态
        Integer integer = taskService.updateSelective(task);
        if (integer>0){
            //获取需求单
            Task task1 = taskService.queryByUUID(taskId);
            //关联修改修改需求单状态
            HouseRenovationOffer offer = new HouseRenovationOffer();
            offer.setHroId(task1.getHroId());
            if ("1".equals(taskStatus)){
                //1.需求单（可以派单） 2.派单中 3.接单  4.已上门 5.报废（客户不同意装修）6.完工
                offer.setHroStatus(1);//只点击收到不改变原需求单的状态，只有分配完成才能修改需求单状态
            }else if ("2".equals(taskStatus)){
                offer.setHroStatus(1);//拒单要把需求单改成可以派单状态
            }
            Integer integer1 = houseRenovationOfferService.updateSelective(offer);
            if (integer1>0){
                map.put("code",200);
            }else{
                map.put("code",101);
            }
        }else{
            map.put("code",101);
        }

        return JSONObject.toJSON(map);
    }
    /**
     * 运营中心》》任务分配(给装修公司)
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/giveTaskToCompany", method = RequestMethod.POST)
    @ResponseBody
    public Object giveTaskToCompany(HttpServletRequest request) throws Exception{
        log.info("giveTaskToCompany");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }

        //获取登录人id
        String uid = request.getHeader("uid");
        //分配任务用户的id
        String toUserId = request.getParameter("toUserId");
        //需求单的id
        String hroId = request.getParameter("hroId");
        //任务id
        String taskId = request.getParameter("taskId");
        //后台派单时间
        String adminGiveTime = request.getParameter("adminGiveTime");

        isNUllMap.put(toUserId,"分配任务用户的id");
        isNUllMap.put(hroId,"需求单的id");
        isNUllMap.put(taskId,"任务的id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskId(KitUtil.uuid());
        task.setTaskFid(taskId);//设置上级任务id
        task.setTaskToUser(toUserId);//任务接收者
        task.setHroId(hroId);//需求单id
        task.setTaskType(2);//1.属于运营中心的任务 2.装修公司 3.工长 4.监理 5.设计师（备用）
        task.setTaskStatus(3);//订单任务状态 1.接收 2.拒绝 3.待接单 4.已完成
        task.setCreateTime(new Date());//分配时间
        task.setOvertimeStatus(2);//1.超时 2.正常
        task.setAdminGiveTime(KitUtil.stringToData(adminGiveTime, "0"));//平台派单时间
        task.setCenterGiveTime(new Date());//中心派单时间
        task.setTaskSidStatus(3);//下级接单状态 1.已接单 2.已拒绝 3.派单中 （这条任务分配给下级的接单状态）

        //保存分配任务
        Integer integer = taskService.saveSelect(task);
        if (integer>0){
            //获取运营中心派发的任务
            Task task1 = taskService.queryByUUID(taskId);
            //关联修改修改需求单状态
            HouseRenovationOffer offer = new HouseRenovationOffer();
            offer.setHroId(task1.getHroId());
            //1.需求单（可以派单） 2.派单中 3.接单  4.已上门 5.报废（客户不同意装修）6.完工
            offer.setHroStatus(3);//只点击收到不改变原需求单的状态，只有分配完成才能修改需求单状态
            Integer integer1 = houseRenovationOfferService.updateSelective(offer);
            if (integer1>0){

                //修改任务状态
                Task task2 = new Task();
                task2.setTaskId(taskId);
                task2.setTaskStatus(1);
                taskService.updateSelective(task2);
                map.put("code",200);
            }else{
                map.put("code",101);
            }
        }else{
            map.put("code",101);
            map.put("msg",StaticFinalVar.ADD_ERR);
        }

        return JSONObject.toJSON(map);
    }

    /**
     * 根据运营中心id查询发给运营中心进行中的任务(已接单的任务)
     * @return
     */
    @RequestMapping(value = "/findCenterGoingTaskList", method = RequestMethod.POST)
    @ResponseBody
    public Object findCenterGoingTaskList(HttpServletRequest request) throws Exception{
        log.info("findCenterGoingTaskList");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        String searchMinDate = request.getParameter("searchMinDate");// 最小时间  yyyy-MM-dd HH:mm:ss
        String searchMaxDate = request.getParameter("searchMaxDate");// 最大时间
        String taskStatus = request.getParameter("taskStatus");// 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）


        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskToUser(uid);
        if (searchMinDate!=null && !"".equals(searchMinDate)){
            task.setSearchMinDate(searchMinDate);
        }
        if (searchMaxDate!=null && !"".equals(searchMaxDate)){
            task.setSearchMaxDate(searchMaxDate);
        }
        if (taskStatus!=null && !"".equals(taskStatus)){
            task.setTaskStatus(Integer.parseInt(taskStatus));
        }

        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            task.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }
        //根据用户id和接单状态查询订单
        List<TaskVo> tasks=taskService.findByUserAndTaskStatus(task);

        //进行中的任务个数
        Integer count = taskService.findCountByGoingTask(uid);
        //判断有无被拒绝的派单
        Integer count1 = taskService.findCountByRefuseTask(uid);

        map.put("code",200);
        map.put("tasks",tasks);
        map.put("goingCount",count);
        map.put("refuseCount",count1);
        return JSONObject.toJSON(map);
    }


    /**
     * 运营中心》》进行中》》已上门
     * @return
     */
    @RequestMapping(value = "/centerVisit", method = RequestMethod.POST)
    @ResponseBody
    public Object centerVisit(HttpServletRequest request) throws Exception{
        log.info("centerVisit");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //任务id
        String taskId = request.getParameter("taskId");
        //接单状态 1.接收 2.拒绝 3.待接单 4.已上门 5.已完成（这条任务的状态）
        String taskStatus = request.getParameter("taskStatus");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(taskId,"任务接收状态");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskId(taskId);
        task.setTaskStatus(Integer.parseInt(taskStatus));
        //根据主键修改任务状态
        Integer integer = taskService.updateSelective(task);
        if (integer>0){
            //获取需求单
            Task task1 = taskService.queryByUUID(taskId);
            //关联修改修改需求单状态
            HouseRenovationOffer offer = new HouseRenovationOffer();
            offer.setHroId(task1.getHroId());
            //1.需求单（可以派单） 2.派单中 3.接单  4.已上门 5.报废（客户不同意装修）6.施工中（创建项目） 7.完工
            offer.setHroStatus(4);
            Integer integer1 = houseRenovationOfferService.updateSelective(offer);
            if (integer1>0){
                map.put("code",200);
            }else{
                map.put("code",101);
            }
        }else{
            map.put("code",101);
        }

        return JSONObject.toJSON(map);
    }


    /**
     * 根据运营中心id查询发给运营中心的已完成任务和筛选（初次进来taskStatus传空）
     * @return
     */
    @RequestMapping(value = "/findCenterFinishTaskList", method = RequestMethod.POST)
    @ResponseBody
    public Object findCenterFinishTaskList(HttpServletRequest request) throws Exception{
        log.info("findCenterFinishTaskList");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        String searchMinDate = request.getParameter("searchMinDate");// 最小时间  yyyy-MM-dd HH:mm:ss
        String searchMaxDate = request.getParameter("searchMaxDate");// 最大时间
        //初次进来taskStatus传空
        String taskStatus = request.getParameter("taskStatus");// 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）

        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskToUser(uid);
        if (searchMinDate!=null && !"".equals(searchMinDate)){
            task.setSearchMinDate(searchMinDate);
        }
        if (searchMaxDate!=null && !"".equals(searchMaxDate)){
            task.setSearchMaxDate(searchMaxDate);
        }
        if (taskStatus!=null && !"".equals(taskStatus)){
            task.setTaskStatus(Integer.parseInt(taskStatus));
        }

        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            task.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }
        //根据用户id查询已完成订单 hro_status 4和5的 4.已上门 5.报废
        List<TaskVo> tasks=taskService.findByUserFinishTask(task);
        map.put("code",200);
        map.put("tasks",tasks);
        return JSONObject.toJSON(map);
    }


    /**
     * 运营中心》》已完成》》作废
     * @return
     */
    @RequestMapping(value = "/centerBlankOut", method = RequestMethod.POST)
    @ResponseBody
    public Object centerBlankOut(HttpServletRequest request) throws Exception{
        log.info("centerBlankOut");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //任务id
        String taskId = request.getParameter("taskId");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(taskId,"任务接收状态");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskId(taskId);
        task.setTaskStatus(5);//接单状态 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）
        //根据主键修改任务状态
        Integer integer = taskService.updateSelective(task);
        if (integer>0){
            //获取需求单
            Task task1 = taskService.queryByUUID(taskId);
            //关联修改修改需求单状态
            HouseRenovationOffer offer = new HouseRenovationOffer();
            offer.setHroId(task1.getHroId());
            //1.需求单（可以派单） 2.派单中 3.接单  4.已上门 5.报废（客户不同意装修）6.施工中（创建项目） 7.完工
            offer.setHroStatus(5);
            Integer integer1 = houseRenovationOfferService.updateSelective(offer);
            if (integer1>0){
                map.put("code",200);
            }else{
                map.put("code",101);
            }
        }else{
            map.put("code",101);
        }

        return JSONObject.toJSON(map);
    }


    /**
     * 运营中心》》已完成》》删除已作废订单
     * @return
     */
    @RequestMapping(value = "/centerDel", method = RequestMethod.POST)
    @ResponseBody
    public Object centerDel(HttpServletRequest request) throws Exception{
        log.info("centerBlankOut");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //任务id
        String taskId = request.getParameter("taskId");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(taskId,"任务接收状态");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        //删除已作废订单
        Integer integer = taskService.deleteByUUId(taskId);
        if (integer>0){
            map.put("code",200);
        }else{
            map.put("code",101);
        }

        return JSONObject.toJSON(map);
    }

    /**
     * 运营中心 》》进行中 》》撤销派单
     * @return
     */
    @RequestMapping(value = "/centerCannel", method = RequestMethod.POST)
    @ResponseBody
    public Object centerCannel(HttpServletRequest request) throws Exception{
        log.info("centerCannel");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //任务id
        String taskId = request.getParameter("taskId");
        //重新指派的用户id
        String taskToUser = request.getParameter("taskToUser");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(taskId,"任务id");
        isNUllMap.put(taskToUser,"重新指派的用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        Task task = new Task();
        task.setTaskId(taskId);
        task.setTaskToUser(taskToUser);
        task.setCenterGiveTime(new Date());

        //重新指派接收人
        Integer integer = taskService.updateSelective(task);
        if (integer>0){
            map.put("code",200);
        }else{
            map.put("code",101);
        }
        return JSONObject.toJSON(map);
    }


    /**
     * 查询装修阶段基础数据
     * @return
     */
    @RequestMapping(value = "/findDecorationStage", method = RequestMethod.POST)
    @ResponseBody
    public Object findDecorationStage(HttpServletRequest request) throws Exception{
        map = new HashMap<String, Object>();

        List<ShopType> ShopType= shopTypeService.findShopTypeByTypeId(StaticFinalVar.DECORATION_STAGE);
        map.put("code",200);
        map.put("ShopType",ShopType);
        return JSONObject.toJSON(map);
    }


    /**
     * 运营中心 》》完成 》》创建项目
     * @return
     */
    @RequestMapping(value = "/centerCreateProject", method = RequestMethod.POST)
    @ResponseBody
    public Object centerCreateProject(HttpServletRequest request) throws Exception{
        log.info("centerCreateProject");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        //需求单id
        String hroId = request.getParameter("hroId");
        //合同图片（最多6张）
        String contractImgs = request.getParameter("contractImgs");
        //设计方id
        String designId = request.getParameter("designId");
        //监理id
        String supervisionId = request.getParameter("supervisionId");

        //json数组 包含阶段的一些信息
        String jsonStr = request.getParameter("jsonStr");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(hroId,"需求单id");
        isNUllMap.put(contractImgs,"合同图片");
        isNUllMap.put(jsonStr,"jsonStr数组");
        isNUllMap.put(designId,"设计方id");
        isNUllMap.put(supervisionId,"监理id");

        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }
        Project p = new Project();
        Date date = new Date();
        String projectId=KitUtil.uuid();
        p.setProId(projectId);
        p.setContractImgs(contractImgs);
        p.setHroId(hroId);
        p.setUserId(uid);//创建人
        p.setCreateTime(date);//创建时间
//        p.setDesignId(designId);
//        p.setSupervisionId(supervisionId);
        Integer integer = projectService.saveSelect(p);

        if (integer>0) {
            //保存设计方的任务
            Task task1 = new Task();
            task1.setTaskId(KitUtil.uuid());
            task1.setTaskToUser(designId);
            task1.setTaskStatus(3);//接单状态 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）
            task1.setHroId(hroId);
            task1.setCreateTime(date);
            task1.setTaskType(5);//1.属于运营中心的任务 2.装修公司 3.工长 4.监理 5.设计方（设计师的公司） 6.设计师
            task1.setProjectId(projectId);
            task1.setOvertimeStatus(2);
            taskService.saveSelect(task1);//保存每个阶段的任务
            //保存监工的任务
            Task task2 = new Task();
            task2.setTaskId(KitUtil.uuid());
            task2.setTaskToUser(supervisionId);
            task2.setTaskStatus(3);//接单状态 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）
            task2.setHroId(hroId);
            task2.setCreateTime(date);
            task2.setTaskType(4);//1.属于运营中心的任务 2.装修公司 3.工长 4.监理 5.设计方（设计师的公司） 6.设计师
            task2.setProjectId(projectId);
            task2.setOvertimeStatus(2);
            taskService.saveSelect(task2);//保存每个阶段的任务

            //解析json数组，将对应的阶段加入到task表中
            //每个数组包含的字段
            JSONArray objects = JSONUtil.parseArray(jsonStr);
            for (int i = 0; i < objects.size(); i++) {
                Task model = new Task();
                cn.hutool.json.JSONObject obj = JSONUtil.parseObj(objects.get(i));
                String taskToUser = (String) obj.get("taskToUser");//任务接收者
                String phaseName = (String) obj.get("phaseName");//阶段（如 水电 等）（保存名称）
                model.setTaskId(KitUtil.uuid());
                model.setTaskToUser(taskToUser);
                model.setTaskStatus(3);//接单状态 1.接收 2.拒绝 3.待接单 4.已上门 5.作废 6.已完成（这条任务的状态）
                model.setPhaseName(phaseName);
                model.setHroId(hroId);
                model.setCreateTime(date);
                model.setTaskType(2);//1.属于运营中心的任务 2.装修公司 3.工长 4.监理 5.设计师（备用）
                model.setProjectId(projectId);
                model.setOvertimeStatus(2);
                taskService.saveSelect(model);//保存每个阶段的任务
            }
        }else{
            return KitUtil.returnMap("101",StaticFinalVar.ADD_ERR);
        }

        return JSONObject.toJSON(map);
    }


    /**
     * 运营中心 》》 进行中 》》查询匹配的装修公司 加入筛选条件 (缺少字段)
     * @return
     */
    @RequestMapping(value = "/centerFindDecorationCompany", method = RequestMethod.POST)
    @ResponseBody
    public Object centerFindDecorationCompany(HttpServletRequest request) throws Exception{
        log.info("centerFindDecorationCompany");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        //筛选条件
        String cityId = request.getParameter("cityId");//城市id
        String isWork = request.getParameter("isWork");//状态1.开工 2.休息

        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }
        CompanyInfo companyInfo = companyInfoService.queryByUUID(uid);

        CompanyVo model = new CompanyVo();
        model.setUserId(uid);
        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            model.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }
        //查询全部就是匹配本地的装修公司
        if (cityId!=null && !"".equals(cityId)){
            model.setCiCity(cityId);
        }else{
            model.setCiCity(companyInfo.getCiCity());
        }

        //开工状态
        if (isWork!=null && !"".equals(isWork)){
            model.setIsWork(Integer.parseInt(isWork));
        }

        List<CompanyVo> companyVoList=companyInfoService.centerFindDecorationCompany(model);

        map.put("code",200);
        map.put("companyVoList",companyVoList);
        return JSONObject.toJSON(map);
    }


}
